package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.TransRecord;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;

/**
 * 批上送IC卡联机交易
 *
 * Created by xiaox on 2017/9/6.
 */
class BatchUploadIccOnlineBuilder extends DataBuilder {
    private TransRecord mTransRecord;
    private TransType mOrgTransType;

    BatchUploadIccOnlineBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
        mTransRecord = mRepository.queryTransRecord(mTransData.getOrgTraceNum());
        mOrgTransType = TransType.values()[mTransRecord.getTransType()];
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setProcCode(mTransData.getTransType().getProcCode());
        sendData.setPan(mTransRecord.getPan());
        //sendData.setAmount(StringUtils.leftPad(mTransRecord.getAmount(), 12, '0'));
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        sendData.setTime(mTransRecord.getTime());
        sendData.setDate(mTransRecord.getDate());
//        sendData.setEntryMode(mTransRecord.getEntryMode());
//        sendData.setCsn(mTransRecord.getCsn());
        //lr 37
        sendData.setRefNum(mTransRecord.getRefNum());

        sendData.setTerminalId(mTransRecord.getTerminalId());
        sendData.setMerchantId(mTransRecord.getMerchantId());
        sendData.setEmvTlv(mTransRecord.getEmvTlv());
        sendData.setField60(formField60());
        sendData.setMac("0000000000000000");
//        sendData.setField62(mTransRecord.getField62());
        return sendData;
    }


    private String formField62() {
        //用法六
        StringBuilder sb = new StringBuilder();
        //第一位取值6表示基于PBOC借/贷记标准的交易明细
        sb.append("6");
        //第二位表示交易笔数，最大值为1
        sb.append("1");
        //卡类型
        if (mTransRecord.getIsInternational()) {
            sb.append("01");
        } else {
            sb.append("00");
        }
        //交易响应码
        sb.append("00");
        //授权金额
        sb.append(mTransRecord.getAmount());
        //授权币种
        sb.append(mTransRecord.getFundType());
        return sb.toString();
    }


    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(SysConstant.LOCALE, "%02d",
                Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "1"))));
        sb.append("0000");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }
}
